/**
 * Copyright (c) 2012, Stereforth John, Brent Mucci, Ken Anderson
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice, 
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 **/

/*
#include "mcl_image.h"
#include "mcl_image_resize.h"
#include "mcl_box_filter.h"

using namespace mcl;

bool ImageResize::resizeBilinear(const Image &source, Image &dest) const
{
	if( source.getWidth() == 0 || source.getHeight() == 0 || 
		dest.getWidth() == 0 || dest.getHeight() == 0 )
		return false;

	int x, y;
	unsigned char *a, *b, *c, *d;
	float x_ratio = ((float)(source.getWidth()-1))/dest.getWidth() ;
	float y_ratio = ((float)(source.getHeight()-1))/dest.getHeight() ;
	float x_diff, y_diff, blue, red, green ;
	float a_weight, b_weight, c_weight, d_weight;
	unsigned int destIndex = 0, sourceIndex = 0 ;

	for (unsigned int i=0;i<dest.getHeight();i++) 
	{
		for (unsigned int j=0;j<dest.getWidth();j++) 
		{
			x = (int)(x_ratio * j) ;
			y = (int)(y_ratio * i) ;
			x_diff = (x_ratio * j) - x ;
			y_diff = (y_ratio * i) - y ;
			sourceIndex = (y*source.getPitch()+x)*4 ;

			a = &source.data()[sourceIndex] ;
			b = &source.data()[sourceIndex+4] ;
			c = &source.data()[sourceIndex+source.getWidth()*4] ;
			d = &source.data()[sourceIndex+source.getWidth()*4+4] ;
			a_weight = (1-x_diff)*(1-y_diff);
			b_weight = (x_diff)*(1-y_diff);
			c_weight = (y_diff)*(1-x_diff);
			d_weight = (x_diff*y_diff);

			for (unsigned int channel=0; channel<4; channel++)
			{
				dest.data()[destIndex+channel] = (int)
					( a[channel]*a_weight + b[channel]*b_weight 
					+ c[channel]*c_weight + d[channel]*d_weight );
			}

			destIndex+=4;
		}
	}
	return true;
}

bool BoxFilter::boxFilter(const Image &source, Image &dest, float* kernel) const
{
	if( source.getWidth() == 0 || source.getHeight() == 0 || source.getWidth() != dest.getWidth() || source.getHeight() != dest.getHeight())
		return false;

    float denominator = 0.0f ;
    float red, green, blue ;
    int ired, igreen, iblue, indexOffset, rgb ;
    int indices[9] = {
        -((int)source.getWidth() + 1),  -(int)source.getWidth(),     -((int)source.getWidth() - 1), 
        -1,                0,           +1, 
        (int)source.getWidth() - 1,      (int)source.getWidth(),      (int)source.getWidth() + 1
    } ;
    for (int i=0;i<9;i++)
        denominator += kernel[i] ;
    if (denominator==0.0f) denominator = 1.0f ;
    for (unsigned int h=1;h<source.getHeight()-1;h++) {
        for (unsigned int w=1;w<source.getWidth()-1;w++) {
            red = green = blue = 0.0f ;
            indexOffset = (h*source.getWidth())+w ;
            for (int k=0;k<9;k++) {
                rgb = source.data()[indexOffset+indices[k]] ;
                red += ((rgb & 0xff0000)>>16)*kernel[k] ;
                green += ((rgb & 0xff00)>>8)*kernel[k] ;
                blue += (rgb & 0xff)*kernel[k] ;
            }            
            ired = (int)(red / denominator) ;
            igreen = (int)(green / denominator) ;
            iblue = (int)(blue / denominator) ;
            if (ired>0xff) ired = 0xff ;
                else if (ired<0) ired = 0 ;
            if (igreen>0xff) igreen = 0xff ;
                else if (igreen<0) igreen = 0 ;
            if (iblue>0xff) iblue = 0xff ;
                else if (iblue<0) iblue = 0 ;            
            dest.data()[indexOffset] = 0xff000000 | ((ired<<16) & 0xff0000) | 
                    ((igreen<<8) & 0xff00) | (iblue & 0xff) ;
        }
    }
	return true;
}*/